<?php


namespace app\admin\model;


use app\admin\validate\Region;

class RegionModel extends Common
{

    const FIELD = ['id', 'name'];
    /**
     * @var string
     */
    protected $name = 'shop_region';

    protected $defaultSoftDelete = 0;

    protected $autoWriteTimestamp = true;

    /**
     * 驼峰格式转换
     * @var bool
     */
    protected $convertNameToCamel = true;

    protected $fail;
    /**
     * 创建店铺市场资源
     * @author ran
     * @date 2021-09-22 16:37
     * mailbox 466180170@qq.com
     */
    public function createShopAdminRegionResource(array $request)
    {
        $validate = new Region();
        if (!$validate->scene('create')->check($request)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $res = $this->create($request);
            if ($res->id) {
                return true;
            }
            $this->error = '店铺大区资源入库失败';
            return false;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
    /**
     * 更新店铺市场资源
     * @author ran
     * @date 2021-09-22 16:37
     * mailbox 466180170@qq.com
     */
    public function updateShopAdminRegionResource(array $request)
    {
        $validate = new Region();
        if (!$validate->scene('update')->check($request)) {
            $this->error = $validate->getError();
            return false;
        }
        try {
            $this->update($request, ['id' => $request['id']]);
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }
    /**
     * 获取 员工信息
     * @param int $id
     * @author ran
     * @date 2021-10-18 9:02
     * mailbox 466180170@qq.com
     */
    public function getShopAdminRegionInfoResource(int $id)
    {
        $field = ['id', 'name'];
        return $this->field($field)->find($id);
    }
    /**
     * 获取店铺大区列表
     * @param int $page
     * @param int $limit
     * @return array
     * @author ran
     * @date 2021-09-23 13:53
     * mailbox 466180170@qq.com
     */
    public function getShopAdminRegionListResource(int $page, int $limit=10):array
    {
        $connect = $this->field(self::FIELD);
        return [
            'list' => $connect->page($page, $limit)->select(),
            'dataCount' => $connect->count()
        ];
    }

}